package com.cssiot.weixin.basic.https;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;
public class TestH {
	
		/**
		 * 使用httpPost  http登陆接口(无http摘要认证)
		 */
		public static void testHttpClient(){
			//1.创建httpClient对象 (使用https协议，需要略过ssl验证，这里使用MySSLSocketFactory)
			HttpClient httpclient = new DefaultHttpClient();
			//url https地址    
			String url ="http://192.168.10.215:8080/tnserver/property_manager/login";
	        try {
	        	//2.创建列参数(httpPost使用)
	        	HttpPost httppost = new HttpPost(url);
	        	List<NameValuePair> list = new ArrayList<NameValuePair>();
	        	//将用户名与密码作为参数写入(username，password这两个参数是   根据你的接口协议而定)
	        	list.add(new BasicNameValuePair("username","admin"));
	        	list.add(new BasicNameValuePair("password","123456"));
	        	//3.写入请求头
	        	httppost.addHeader("Content-type", "application/x-www-form-urlencoded"); 
	        	//4.将参数写入到httpost中，并设置编码格式为UTF-8
	        	UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
	        	httppost.setEntity(entity);
				//5.执行
	        	HttpResponse post= httpclient.execute(httppost);
	        	//6.执行完毕，判断返回code码，200为正常
	            if (post.getStatusLine().getStatusCode() == 200) {  
	            	//7.获取返回json格式数据  注意，这里不能直接转java.lang.String进行强转，
	            	String conResult =  EntityUtils.toString(post.getEntity());
	            	//8.处理返回数据....(完)
	            	JSONObject sobj = new JSONObject();  
	                sobj = JSONObject.fromObject(conResult);
	                System.out.println(sobj);
	            } else {  
	               System.out.println("失败....");
	            }
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
		}
}

